package com.imooc.oo

object ConstructorApp {

  def main(args: Array[String]): Unit = {
//    val person = new Person("zs",30)
//
//    println(person.gender)
//
//    val p = new Person("zs",30,"m")
//
//    println(p.gender)


    val student = new Student("PK",18,"Math")
    println(student.name + ":" + student.major)


    println(student.toString)

    println(student.school)



  }

}

// 主构造器
class Person(val name:String,val age:Int) {
  println("Person Constructor center ... ")

  val school = "ustc"
  var gender: String = _

  def this(name: String,age:Int,gender:String){
    //这里一定要指定一个构造器 无论 主构造器 还是 附属构造器
    this(name,age)
    this.gender = gender
  }

  println("Person Constructor leave ... ")
}


/**
  * 当子类继承父类时，如果子类的属性 父类中没有时 , 需要声明 var or val 关键字
  * @param name
  * @param age
  * @param major
  */
class Student(name: String,age: Int,var major: String) extends Person(name,age){

  println("Student Constructor center ... ")

  override val school = "peking"

  override def toString():String = " Person: override def toString "

  println("Student Constructor center ... ")

}
